package com.oracle.service.impl;

import com.oracle.mapper.CommentMapper;
import com.oracle.mapper.NewsMapper;
import com.oracle.pojo.Comment;
import com.oracle.pojo.News;
import com.oracle.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<News> getAllNews() {
        return newsMapper.selectAllNews();
    }

    @Override
    public boolean addNews(News news) {
        return newsMapper.insertNews(news)>0;
    }

    @Override
    public News getNewsById(Integer id) {
        List<Comment> comments = commentMapper.selectCommentsByNid(id);
        News news = newsMapper.selectNewsById(id);
        news.setComments(comments);
        return news;
    }

    @Override
    public boolean deleteNewsById(Integer id) {
        int c = commentMapper.deleteCommentByNid(id);
        int n = newsMapper.deleteNewsById(id);
        if (c>0 && n>0){
            return true;
        }else {
            return false;
        }
    }
}
